/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.fd.service.impl;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;


import com.xy.biz.uc.client.UserClient;
import com.xy.lang.util.CalendarUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xy.lang.domain.Result;
import com.xy.biz.fd.domain.bo.DocumentLogBO;
import com.xy.biz.fd.domain.query.DocumentLogQuery;
import com.xy.biz.fd.service.DocumentLogService;
import com.xy.biz.fd.manager.DocumentLogManager;

/**
 *
 *
 * @author zgy
 * @date 2021-5-20
 */
@Service
public class DocumentLogServiceImpl implements DocumentLogService {

    @Autowired
    private DocumentLogManager documentLogManager;
    @Autowired
    private UserClient userClient;

    @Override
    public Result<?> saveDocumentLog(DocumentLogBO documentLogBO) {
        int affectedRows = documentLogManager.saveDocumentLog(documentLogBO);
        if (affectedRows > 0) {
            return Result.success();
        }
        return Result.ofErrorRowsAffected();
    }

    @Override
    public Result<?> saveAllDocumentLog(Collection<DocumentLogBO> collection) {
        int affectedRows = documentLogManager.saveAllDocumentLog(collection);
        if (affectedRows > 0) {
            return Result.success();
        }
        return Result.ofErrorRowsAffected();
    }

    @Override
    public Result<?> updateDocumentLog(DocumentLogBO documentLogBO) {
        int affectedRows = documentLogManager.updateDocumentLog(documentLogBO);
        if (affectedRows > 0) {
            return Result.success();
        }
        return Result.ofErrorRowsAffected();
    }

    @Override
    public Result<?> deleteDocumentLog(Long documentLogId) {
        int affectedRows = documentLogManager.deleteDocumentLog(documentLogId);
        if (affectedRows > 0) {
            return Result.success();
        }
        return Result.ofErrorRowsAffected();
    }

    @Override
    public DocumentLogBO getDocumentLog(Long documentLogId) {
        return documentLogManager.getDocumentLog(documentLogId);
    }

    @Override
    public List<DocumentLogBO> listDocumentLogById(Collection<Long> collection) {
        return documentLogManager.listDocumentLogById(collection);
    }

    @Override
    public List<DocumentLogBO> listDocumentLog(DocumentLogQuery query) {
        List<DocumentLogBO> documentLogBOS = documentLogManager.listDocumentLog(query);
        userClient.buildByUser(documentLogBOS,
                documentLogBOS.stream().map(DocumentLogBO::getCreateUserId).collect(Collectors.toSet()),
                DocumentLogBO::getCreateUserId,
                (doc, userDTO) -> doc.setCreateUserName(userDTO.getUserName()));
        documentLogBOS.sort(Comparator.comparing(DocumentLogBO::getCreateTime).reversed());
        documentLogBOS.forEach(doc -> doc.setCreateTimeString(CalendarUtils.getDeadlineString(doc.getCreateTime(), LocalDateTime.now())));
        return documentLogBOS;
    }

    @Override
    public int countDocumentLog(DocumentLogQuery query) {
        return documentLogManager.countDocumentLog(query);
    }

}
